#pragma once
//文件版本
#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include <iostream>
#include <string>
#include <assert.h>
#include <unordered_map>
#include <vector>
#include <fstream>

//根据list文件，加载所有题目信息到内存中
//model：主要用来和数据进行交互，对外提供访问数据接口


namespace NS_model
{
    using namespace std;
    using namespace NS_log;
    using namespace NS_util;
    struct Question
    {
        string number;//题目编号
        string title;//题目标题
        string star; //难度
        int cpu_limit; //时间要求
        int mem_limit; //空间要求
        string desc; //题目的描述
        string header;//题目预设编辑器代码
        string tail; //题目的测试用例
    };
    const std::string questions_list = "./questions/questions.list";
    const std::string questions_path = "./questions/";
    class Model
    {
    private:
        unordered_map<string,Question> _questions;//string 编号
    public:
        Model()
        {
            assert(LoadQuestionList(questions_list));
        }
        bool LoadQuestionList(const std::string &question_list)
        {
            //加载配置文件，questions/questions.list+题目编号文件
            ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，检查是否存在题库文件" << "\n";
                return false;
            }
            std::string line;
            while(getline(in,line))
            {
                vector<string> tokens;
                Stringutil::SpiltString(line,&tokens," ");
                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，检查文件格式" << "\n";
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = std::stoi(tokens[3]);
                q.mem_limit = std::stoi(tokens[4]);

                string path = questions_path;
                path += q.number;
                path += "/";

                FileUtil::Readfile(path+"desc.txt",&(q.desc),true);
                FileUtil::Readfile(path+"header.cpp",&(q.header),true);
                FileUtil::Readfile(path+"tail.cpp",&(q.tail),true);

                _questions.insert(make_pair(q.number,q));

            }

            LOG(INFO) << "加载题库成功" << "\n";
            in.close();
            return true;
        }
        bool GetAllQuestions(vector<Question> *out) //获取全部题目
        {
            if(_questions.size() == 0)
            {
                LOG(ERROR) << "获取全部题目失败" << "\n";
                return false;
            }
            for(const auto &q : _questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestion(const std::string &number,Question *q) //获取一个题目
        {
            const auto& iter = _questions.find(number);
            if(iter == _questions.end())
            {
                LOG(ERROR) << "获取题目失败,题目编号：" << number << "\n";
                return false;
            }
            (*q) = iter->second;
            return true;
        }
        ~Model()
        {}

    };
}